package com.mjf.cjt.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chanjet.openapi.sdk.java.exception.ChanjetApiException;
import com.mjf.cjt.domain.PurchaseOrder;
import com.mjf.cjt.domain.PurchaseOrderDetails;
import com.mjf.cjt.domain.TCjtExpensevoucher;
import com.mjf.cjt.domain.TCjtExpensevoucherDetails;
import com.mjf.cjt.mapper.PurchaseOrderMapper;
import com.mjf.cjt.service.AsyncTMService;
import com.mjf.cjt.service.PurchaseOrderDetailsService;
import com.mjf.cjt.service.PurchaseOrderService;
import com.mjf.cjt.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
/**
 * <p>
 * 采购订单列表同步
 * </p>
 *
 * @author
 * @since 2025-04-05
 */
@Service
public class PurchaseOrderServiceImpl extends ServiceImpl<PurchaseOrderMapper, PurchaseOrder> implements PurchaseOrderService {

    @Autowired
    private AsyncTMService asyncTMService;


    @Autowired
    private PurchaseOrderDetailsService purchaseOrderDetailsService;

    @Override
    public Page<PurchaseOrderListVo> queryPurchaseOrder(PurchaseOrderQueryParam param) {

        Page<PurchaseOrderListVo> page = new Page<>(param.getPage(), param.getSize());
        page.setTotal(0);
        page.setRecords(new ArrayList<>());
        LambdaQueryWrapper<PurchaseOrder> qw = Wrappers.<PurchaseOrder>lambdaQuery()
                .ge(StrUtil.isNotBlank(param.getStart()), PurchaseOrder::getCreatedTime, param.getStart())
                .le(StrUtil.isNotBlank(param.getEnd()), PurchaseOrder::getCreatedTime, param.getEnd())
                ;
        long count = count(qw);
        page.setTotal(count);
        qw.orderByDesc(PurchaseOrder::getCreatedTime);
        qw.last("limit " + (param.getPage() - 1) * param.getSize() + "," + param.getSize());

        List<PurchaseOrder> list = list(qw);
        page.setRecords(list.stream().map(item -> PurchaseOrderListVo.builder()
                        .purchaseOrderId(item.getPurchaseOrderId())
                        .voucherstateName(item.getVoucherstateName())
                        .purchaseOrderAllstate(item.getPurchaseOrderAllstate())
                        .voucherdate(item.getVoucherdate())
                        .code(item.getCode())
                        .businessTypeName(item.getBusinessTypeName())
                        .partnerCode(item.getPartnerCode())
                        .partnerName(item.getPartnerName())
                        .partnerAbbName(item.getPartnerAbbName())
                        .partnerPriuserdefnvc1(item.getPartnerPriuserdefnvc1())
                        .partnerPriuserdefnvc2(item.getPartnerPriuserdefnvc2())
                        .partnerPriuserdefnvc3(item.getPartnerPriuserdefnvc3())
                        .partnerPriuserdefnvc4(item.getPartnerPriuserdefnvc4())
                        .partnerPriuserdefnvc5(item.getPartnerPriuserdefnvc5())
                        .partnerPriuserdefdecm1(item.getPartnerPriuserdefdecm1())
                        .partnerPriuserdefdecm2(item.getPartnerPriuserdefdecm2())
                        .partnerPriuserdefdecm3(item.getPartnerPriuserdefdecm3())
                        .partnerPriuserdefdecm4(item.getPartnerPriuserdefdecm4())
                        .partnerPriuserdefdecm5(item.getPartnerPriuserdefdecm5())
                        .deptName(item.getDeptName())
                        .clerkName(item.getClerkName())
                        .projectName(item.getProjectName())
                        .exchangeRate(item.getExchangeRate())
                        .currencyName(item.getCurrencyName())
                        .acceptDate(item.getAcceptDate())
                        .deliverymodeName(item.getDeliverymodeName())
                        .acceptAddress(item.getAcceptAddress())
                        .transdocno(item.getTransdocno())
                        .logisticsCampanyName(item.getLogisticsCampanyName())
                        .shipstatusName(item.getShipstatusName())
                        .linkman(item.getLinkman())
                        .linktelphone(item.getLinktelphone())
                        .contractId(item.getContractId())
                        .paytypeName(item.getPaytypeName())
                        .warehouseName(item.getWarehouseName())
                        .discountRate(item.getDiscountRate())
                        .saleorderCode(item.getSaleorderCode())
                        .invoiceCrossCheckstatusName(item.getInvoiceCrossCheckstatusName())
                        .purchaseInvoiceCode(item.getPurchaseInvoiceCode())
                        .purchaseInvoiceNo(item.getPurchaseInvoiceNo())
                        .prePurchaseInvoiceCode(item.getPrePurchaseInvoiceCode())
                        .prePurchaseInvoiceNo(item.getPrePurchaseInvoiceNo())
                        .invoiceCrossCheckAmount(item.getInvoiceCrossCheckAmount())
                        .memo(item.getMemo())
                        .maker(item.getMaker())
                        .agency(item.getAgency())
                        .reviser(item.getReviser())
                        .auditor(item.getAuditor())
                        .auditedDate(item.getAuditedDate())
                        .auditedTime(item.getAuditedTime())
                        .printcount(item.getPrintcount())
                        .changer(item.getChanger())
                        .changeDate(item.getChangeDate())
                        .closedBy(item.getClosedBy())
                        .closedTime(item.getClosedTime())
                        .origPrepaymentAmount(item.getOrigPrepaymentAmount())
                        .origTotalAmount(item.getOrigTotalAmount())
                        .origTotalTaxamount(item.getOrigTotalTaxamount())
                        .totalAmount(item.getTotalAmount())
                        .totalTaxamount(item.getTotalTaxamount())
                        .externalCode(item.getExternalCode())
                        .sourceMarketingOrganCode(item.getSourceMarketingOrganCode())
                        .sourceMarketingOrganName(item.getSourceMarketingOrganName())
                        .sourceVouchertypeName(item.getSourceVouchertypeName())
                        .sourceVoucherCode(item.getSourceVoucherCode())
                        .confirmConsignor(item.getConfirmConsignor())
                        .confirmConsignorTime(item.getConfirmConsignorTime())
                        .collaborateVoucherCode(item.getCollaborateVoucherCode())
                        .isstockRequestAutotransfer(item.getIsstockRequestAutotransfer())
                        .priuserdefnvc1(item.getPriuserdefnvc1())
                        .priuserdefnvc2(item.getPriuserdefnvc2())
                        .priuserdefnvc3(item.getPriuserdefnvc3())
                        .priuserdefnvc4(item.getPriuserdefnvc4())
                        .priuserdefnvc5(item.getPriuserdefnvc5())
                        .priuserdefnvc6(item.getPriuserdefnvc6())
                        .priuserdefdecm1(item.getPriuserdefdecm1())
                        .priuserdefdecm2(item.getPriuserdefdecm2())
                        .priuserdefdecm3(item.getPriuserdefdecm3())
                        .priuserdefdecm4(item.getPriuserdefdecm4())
                        .priuserdefdecm5(item.getPriuserdefdecm5())
                        .priuserdefdecm6(item.getPriuserdefdecm6())
                        .pubuserdefnvc1(item.getPubuserdefnvc1())
                        .pubuserdefnvc2(item.getPubuserdefnvc2())
                        .pubuserdefnvc3(item.getPubuserdefnvc3())
                        .pubuserdefnvc4(item.getPubuserdefnvc4())
                        .pubuserdefnvc5(item.getPubuserdefnvc5())
                        .pubuserdefnvc6(item.getPubuserdefnvc6())
                        .pubuserdefdecm1(item.getPubuserdefdecm1())
                        .pubuserdefdecm2(item.getPubuserdefdecm2())
                        .pubuserdefdecm3(item.getPubuserdefdecm3())
                        .pubuserdefdecm4(item.getPubuserdefdecm4())
                        .pubuserdefdecm5(item.getPubuserdefdecm5())
                        .pubuserdefdecm6(item.getPubuserdefdecm6())
                        .ispriceCheck(item.getIspriceCheck())
                        .createdTime(item.getCreatedTime())
                        .socialCollaborateFlag(item.getSocialCollaborateFlag())
                        .socialCollaborateStatusName(item.getSocialCollaborateStatusName())
                        .ts(item.getTs())
                        .busopAttachCount(item.getBusopAttachCount())
                        .build())
                .collect(Collectors.toList()));
        return page;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void getPurchaseOrderFromRemote(String start, String end) {
        try {
            List<PurchaseOrderVo> saleOrderRemote = asyncTMService.getPurchaseOrderFromRemote(start, end);
            if (CollectionUtil.isNotEmpty(saleOrderRemote)) {
                // 按明细行ID分组
                Map<Long, List<PurchaseOrderVo>> voMap =
                        saleOrderRemote.stream().collect(Collectors.groupingBy(PurchaseOrderVo::getId));

                List<PurchaseOrder> purchaseOrder = new ArrayList<>();

                List<PurchaseOrderDetails> purchaseOrderDetails = new ArrayList<>();
                for (Map.Entry<Long, List<PurchaseOrderVo>> entry : voMap.entrySet()) {
                    // 主单
                    PurchaseOrder order = new PurchaseOrder();
                    order.setPurchaseOrderId(entry.getValue().get(0).getId());
                    order.setVoucherstateName(entry.getValue().get(0).getVoucherstateName());
                    order.setPurchaseOrderAllstate(entry.getValue().get(0).getPurchaseorderallstate());
                    order.setVoucherdate(entry.getValue().get(0).getVoucherdate());
                    order.setCode(entry.getValue().get(0).getCode());
                    order.setBusinessTypeName(entry.getValue().get(0).getBusinesstypeName());
                    order.setPartnerCode(entry.getValue().get(0).getPartnerCode());
                    order.setPartnerName(entry.getValue().get(0).getPartnerName());
                    order.setPartnerAbbName(entry.getValue().get(0).getPartnerabbname());
                    order.setPartnerPriuserdefnvc1(entry.getValue().get(0).getPartnerpriuserdefnvc1());
                    order.setPartnerPriuserdefnvc2(entry.getValue().get(0).getPartnerpriuserdefnvc2());
                    order.setPartnerPriuserdefnvc3(entry.getValue().get(0).getPartnerpriuserdefnvc3());
                    order.setPartnerPriuserdefnvc4(entry.getValue().get(0).getPartnerpriuserdefnvc4());
                    order.setPartnerPriuserdefnvc5(entry.getValue().get(0).getPartnerpriuserdefnvc5());
                    order.setPartnerPriuserdefdecm1(Objects.isNull(entry.getValue().get(0).getPartnerpriuserdefdecm1())? null :new BigDecimal(entry.getValue().get(0).getPartnerpriuserdefdecm1()));
                    order.setPartnerPriuserdefdecm2(Objects.isNull(entry.getValue().get(0).getPartnerpriuserdefdecm2())? null :new BigDecimal(entry.getValue().get(0).getPartnerpriuserdefdecm2()));
                    order.setPartnerPriuserdefdecm3(Objects.isNull(entry.getValue().get(0).getPartnerpriuserdefdecm3())? null :new BigDecimal(entry.getValue().get(0).getPartnerpriuserdefdecm3()));
                    order.setPartnerPriuserdefdecm4(Objects.isNull(entry.getValue().get(0).getPartnerpriuserdefdecm4())? null :new BigDecimal(entry.getValue().get(0).getPartnerpriuserdefdecm4()));
                    order.setPartnerPriuserdefdecm5(Objects.isNull(entry.getValue().get(0).getPartnerpriuserdefdecm5())? null :new BigDecimal(entry.getValue().get(0).getPartnerpriuserdefdecm5()));
                    order.setDeptName(entry.getValue().get(0).getDepartmentName());
                    order.setClerkName(entry.getValue().get(0).getClerkName());
                    order.setProjectName(entry.getValue().get(0).getProjectName());
                    order.setExchangeRate(entry.getValue().get(0).getExchangerate());
                    order.setCurrencyName(entry.getValue().get(0).getCurrencyName());
                    order.setAcceptDate(entry.getValue().get(0).getAcceptdate());
                    order.setDeliverymodeName(entry.getValue().get(0).getDeliverymodeName());
                    order.setAcceptAddress(entry.getValue().get(0).getAcceptaddress());
                    order.setTransdocno(entry.getValue().get(0).getTransdocno());
                    order.setLogisticsCampanyName(entry.getValue().get(0).getLogisticscampanyName());
                    order.setShipstatusName(entry.getValue().get(0).getShipstatusName());
                    order.setLinkman(entry.getValue().get(0).getLinkman());
                    order.setLinktelphone(entry.getValue().get(0).getLinktelphone());
                    order.setContractId(entry.getValue().get(0).getContractid());
                    order.setPaytypeName(entry.getValue().get(0).getPaytypeName());
                    order.setWarehouseName(entry.getValue().get(0).getWarehouseName());
                    order.setDiscountRate(entry.getValue().get(0).getDiscountrate());
                    order.setSaleorderCode(entry.getValue().get(0).getSaleordercode());
                    order.setInvoiceCrossCheckstatusName(entry.getValue().get(0).getInvoicecrosscheckstatusName());
                    order.setPurchaseInvoiceCode(entry.getValue().get(0).getPurchaseinvoicecode());
                    order.setPurchaseInvoiceNo(entry.getValue().get(0).getPurchaseinvoiceno());
                    order.setPrePurchaseInvoiceCode(entry.getValue().get(0).getPrepurchaseinvoicecode());
                    order.setPrePurchaseInvoiceNo(entry.getValue().get(0).getPrepurchaseinvoiceno());
                    order.setInvoiceCrossCheckAmount(Objects.isNull(entry.getValue().get(0).getInvoicecrosscheckamount())? null :new BigDecimal(entry.getValue().get(0).getInvoicecrosscheckamount()));
                    order.setMemo(entry.getValue().get(0).getMemo());
                    order.setMaker(entry.getValue().get(0).getMaker());
                    order.setAgency(entry.getValue().get(0).getAgency());
                    order.setAuditor(entry.getValue().get(0).getAuditor());
                    order.setAuditedDate(entry.getValue().get(0).getAuditeddate());
                    order.setAuditedTime(entry.getValue().get(0).getAuditedtime());
                    order.setPrintcount(entry.getValue().get(0).getPrintcount());
                    order.setChanger(entry.getValue().get(0).getChanger());
                    order.setChangeDate(entry.getValue().get(0).getChangedate());
                    order.setClosedBy(entry.getValue().get(0).getClosedby());
                    order.setClosedTime(entry.getValue().get(0).getClosedtime());
                    order.setOrigPrepaymentAmount(Objects.isNull(entry.getValue().get(0).getOrigprepaymentamount())? null :new BigDecimal(entry.getValue().get(0).getOrigprepaymentamount()));
                    order.setOrigTotalAmount(Objects.isNull(entry.getValue().get(0).getOrigtotalamount())? null :new BigDecimal(entry.getValue().get(0).getOrigtotalamount()));
                    order.setOrigTotalTaxamount(Objects.isNull(entry.getValue().get(0).getOrigtotaltaxamount())? null :new BigDecimal(entry.getValue().get(0).getOrigtotaltaxamount()));
                    order.setTotalAmount(Objects.isNull(entry.getValue().get(0).getTotalamount())? null :new BigDecimal(entry.getValue().get(0).getTotalamount()));
                    order.setTotalTaxamount(Objects.isNull(entry.getValue().get(0).getTotaltaxamount())? null :new BigDecimal(entry.getValue().get(0).getTotaltaxamount()));
                    order.setExternalCode(entry.getValue().get(0).getExternalcode());
                    order.setSourceMarketingOrganCode(entry.getValue().get(0).getSourcemarketingorganCode());
                    order.setSourceMarketingOrganName(entry.getValue().get(0).getSourcemarketingorganName());
                    order.setSourceVouchertypeName(entry.getValue().get(0).getSourcevouchertypeName());
                    order.setSourceVoucherCode(entry.getValue().get(0).getSourcevouchercode());
                    order.setConfirmConsignor(entry.getValue().get(0).getConfirmconsignor());
                    order.setConfirmConsignorTime(entry.getValue().get(0).getConfirmconsignortime());
                    order.setCollaborateVoucherCode(entry.getValue().get(0).getCollaboratevouchercode());
                    order.setIsstockRequestAutotransfer(entry.getValue().get(0).getIsstockrequestautotransfer());
                    order.setPriuserdefnvc1(entry.getValue().get(0).getPriuserdefnvc1());
                    order.setPriuserdefnvc2(entry.getValue().get(0).getPriuserdefnvc2());

                    order.setPriuserdefnvc3(entry.getValue().get(0).getPriuserdefnvc3());
                    order.setPriuserdefnvc4(entry.getValue().get(0).getPriuserdefnvc4());
                    order.setPriuserdefnvc5(entry.getValue().get(0).getPriuserdefnvc5());
                    order.setPriuserdefnvc6(entry.getValue().get(0).getPriuserdefnvc6());
                    order.setPriuserdefdecm1(Objects.isNull(entry.getValue().get(0).getPriuserdefdecm1())? null :new BigDecimal(entry.getValue().get(0).getPriuserdefdecm1()));
                    order.setPriuserdefdecm2(Objects.isNull(entry.getValue().get(0).getPriuserdefdecm2())? null :new BigDecimal(entry.getValue().get(0).getPriuserdefdecm2()));
                    order.setPriuserdefdecm3(Objects.isNull(entry.getValue().get(0).getPriuserdefdecm3())? null :new BigDecimal(entry.getValue().get(0).getPriuserdefdecm3()));
                    order.setPriuserdefdecm4(Objects.isNull(entry.getValue().get(0).getPriuserdefdecm4())? null :new BigDecimal(entry.getValue().get(0).getPriuserdefdecm4()));
                    order.setPriuserdefdecm5(Objects.isNull(entry.getValue().get(0).getPriuserdefdecm5())? null :new BigDecimal(entry.getValue().get(0).getPriuserdefdecm5()));
                    order.setPriuserdefdecm6(Objects.isNull(entry.getValue().get(0).getPriuserdefdecm6())? null :new BigDecimal(entry.getValue().get(0).getPriuserdefdecm6()));
                    order.setPubuserdefnvc1(entry.getValue().get(0).getPubuserdefnvc1());
                    order.setPubuserdefnvc2(entry.getValue().get(0).getPubuserdefnvc2());
                    order.setPubuserdefnvc3(entry.getValue().get(0).getPubuserdefnvc3());
                    order.setPubuserdefnvc4(entry.getValue().get(0).getPubuserdefnvc4());
                    order.setPubuserdefnvc5(entry.getValue().get(0).getPubuserdefnvc5());
                    order.setPubuserdefnvc6(entry.getValue().get(0).getPubuserdefnvc6());
                    order.setPubuserdefdecm1(Objects.isNull(entry.getValue().get(0).getPubuserdefdecm1())? null :new BigDecimal(entry.getValue().get(0).getPubuserdefdecm1()));
                    order.setPubuserdefdecm2(Objects.isNull(entry.getValue().get(0).getPubuserdefdecm2())? null :new BigDecimal(entry.getValue().get(0).getPubuserdefdecm2()));
                    order.setPubuserdefdecm3(Objects.isNull(entry.getValue().get(0).getPubuserdefdecm3())? null :new BigDecimal(entry.getValue().get(0).getPubuserdefdecm3()));
                    order.setPubuserdefdecm4(Objects.isNull(entry.getValue().get(0).getPubuserdefdecm4())? null :new BigDecimal(entry.getValue().get(0).getPubuserdefdecm4()));
                    order.setPubuserdefdecm5(Objects.isNull(entry.getValue().get(0).getPubuserdefdecm5())? null :new BigDecimal(entry.getValue().get(0).getPubuserdefdecm5()));
                    order.setPubuserdefdecm6(Objects.isNull(entry.getValue().get(0).getPubuserdefdecm6())? null :new BigDecimal(entry.getValue().get(0).getPubuserdefdecm6()));
                    order.setIspriceCheck(entry.getValue().get(0).getIspricecheck());
                    order.setCreatedTime(entry.getValue().get(0).getCreatedtime());
                    order.setSocialCollaborateFlag(entry.getValue().get(0).getSocialcollaborateflag());
                    order.setSocialCollaborateStatusName(entry.getValue().get(0).getSocialcollaboratestatusName());
                    order.setTs(entry.getValue().get(0).getTs());
                    order.setBusopAttachCount(entry.getValue().get(0).getBusopattachcount());
                    purchaseOrder.add(order);

                    // 按明细ID分组
                    Map<Long, List<PurchaseOrderVo>> detailsMap = entry.getValue().stream().collect(Collectors.groupingBy(PurchaseOrderVo::getPurchaseorderdetails_id));
                    for (Map.Entry<Long, List<PurchaseOrderVo>> detailsEntry : detailsMap.entrySet()) {
                        // 明细
                        PurchaseOrderVo orderDetail = detailsEntry.getValue().get(0);
                        PurchaseOrderDetails details = new PurchaseOrderDetails();
                        details.setWarehouseName(orderDetail.getPurchaseorderdetails_warehouse_name());
                        details.setInventoryBarcode(orderDetail.getPurchaseorderdetails_inventorybarcode());
                        details.setProjectName(orderDetail.getPurchaseorderdetails_project_name());
                        details.setPartnerInventoryName(orderDetail.getPurchaseorderdetails_partnerinventoryname());
                        details.setPartnerInventoryCode(orderDetail.getPurchaseorderdetails_partnerinventorycode());
                        details.setInventoryName(orderDetail.getPurchaseorderdetails_inventory_name());
                        details.setInventoryCode(orderDetail.getPurchaseorderdetails_inventory_code());
                        details.setInventorySpecification(orderDetail.getPurchaseorderdetails_inventory_specification());
                        details.setInventoryPriuserdefnvc1(orderDetail.getPurchaseorderdetails_inventory_priuserdefnvc1());
                        details.setInventoryPriuserdefnvc2(orderDetail.getPurchaseorderdetails_inventory_priuserdefnvc2());
                        details.setInventoryPriuserdefnvc3(orderDetail.getPurchaseorderdetails_inventory_priuserdefnvc3());
                        details.setInventoryPriuserdefnvc4(orderDetail.getPurchaseorderdetails_inventory_priuserdefnvc4());
                        details.setInventoryPriuserdefnvc5(orderDetail.getPurchaseorderdetails_inventory_priuserdefnvc5());
                        details.setInventoryPriuserdefdecm1(Objects.isNull(orderDetail.getPurchaseorderdetails_inventory_priuserdefdecm1())? null :new BigDecimal(orderDetail.getPurchaseorderdetails_inventory_priuserdefdecm1()));
                        details.setInventoryPriuserdefdecm2(Objects.isNull(orderDetail.getPurchaseorderdetails_inventory_priuserdefdecm2())? null :new BigDecimal(orderDetail.getPurchaseorderdetails_inventory_priuserdefdecm2()));
                        details.setInventoryPriuserdefdecm3(Objects.isNull(orderDetail.getPurchaseorderdetails_inventory_priuserdefdecm3())? null :new BigDecimal(orderDetail.getPurchaseorderdetails_inventory_priuserdefdecm3()));
                        details.setInventoryPriuserdefdecm4(Objects.isNull(orderDetail.getPurchaseorderdetails_inventory_priuserdefdecm4())? null :new BigDecimal(orderDetail.getPurchaseorderdetails_inventory_priuserdefdecm4()));
                        details.setInventoryPriuserdefdecm5(Objects.isNull(orderDetail.getPurchaseorderdetails_inventory_priuserdefdecm5())? null :new BigDecimal(orderDetail.getPurchaseorderdetails_inventory_priuserdefdecm5()));
                        details.setUnitName(orderDetail.getPurchaseorderdetails_unit_name());
                        details.setQuantity(Objects.isNull(orderDetail.getPurchaseorderdetails_quantity())? null :new BigDecimal(orderDetail.getPurchaseorderdetails_quantity()));
                        details.setUnit2Name(orderDetail.getPurchaseorderdetails_unit2_name());
                        details.setQuantity2(Objects.isNull(orderDetail.getPurchaseorderdetails_quantity2())? null :new BigDecimal(orderDetail.getPurchaseorderdetails_quantity2()));
                        details.setCompositionQuantity(orderDetail.getPurchaseorderdetails_compositionquantity());
                        details.setRetailPrice(Objects.isNull(orderDetail.getPurchaseorderdetails_retailprice())? null :new BigDecimal(orderDetail.getPurchaseorderdetails_retailprice()));
                        details.setOrigPrice(Objects.isNull(orderDetail.getPurchaseorderdetails_origprice())? null :new BigDecimal(orderDetail.getPurchaseorderdetails_origprice()));
                        details.setLatestPprice(Objects.isNull(orderDetail.getPurchaseorderdetails_latestppric2())? null :new BigDecimal(orderDetail.getPurchaseorderdetails_latestppric2()));
                        details.setDiscountrate(orderDetail.getPurchaseorderdetails_discountrate());
                        details.setOrigDiscountPrice(Objects.isNull(orderDetail.getPurchaseorderdetails_origdiscountprice())? null :new BigDecimal(orderDetail.getPurchaseorderdetails_origdiscountprice()));
                        details.setTaxrate(orderDetail.getPurchaseorderdetails_taxrate());
                        details.setOrigTaxPrice(Objects.isNull(orderDetail.getPurchaseorderdetails_origtaxprice())? null :new BigDecimal(orderDetail.getPurchaseorderdetails_origtaxprice()));
                        details.setOrigDiscountAmount(Objects.isNull(orderDetail.getPurchaseorderdetails_origdiscountamount())? null :new BigDecimal(orderDetail.getPurchaseorderdetails_origdiscountamount()));
                        details.setOrigTax(Objects.isNull(orderDetail.getPurchaseorderdetails_origtax())? null :new BigDecimal(orderDetail.getPurchaseorderdetails_origtax()));
                        details.setOrigTaxAmount(Objects.isNull(orderDetail.getPurchaseorderdetails_origtaxamount())? null :new BigDecimal(orderDetail.getPurchaseorderdetails_origtaxamount()));
                        details.setOrigDiscount(Objects.isNull(orderDetail.getPurchaseorderdetails_origdiscount())? null :new BigDecimal(orderDetail.getPurchaseorderdetails_origdiscount()));
                        details.setDiscountPrice(Objects.isNull(orderDetail.getPurchaseorderdetails_discountprice())? null :new BigDecimal(orderDetail.getPurchaseorderdetails_discountprice()));
                        details.setTaxPrice(Objects.isNull(orderDetail.getPurchaseorderdetails_taxprice())? null :new BigDecimal(orderDetail.getPurchaseorderdetails_taxprice()));
                        details.setDiscountAmount(Objects.isNull(orderDetail.getPurchaseorderdetails_discountamount())? null :new BigDecimal(orderDetail.getPurchaseorderdetails_discountamount()));
                        details.setTax(Objects.isNull(orderDetail.getPurchaseorderdetails_tax())? null :new BigDecimal(orderDetail.getPurchaseorderdetails_tax()));
                        details.setTaxAmount(Objects.isNull(orderDetail.getPurchaseorderdetails_taxamount())? null :new BigDecimal(orderDetail.getPurchaseorderdetails_taxamount()));
                        details.setDiscount(Objects.isNull(orderDetail.getPurchaseorderdetails_discount())? null :new BigDecimal(orderDetail.getPurchaseorderdetails_discount()));
                        details.setAcceptdate(orderDetail.getPurchaseorderdetails_acceptdate());
                        details.setIspresent(orderDetail.getPurchaseorderdetails_ispresent());
                        details.setInmeterialInspection(orderDetail.getPurchaseorderdetails_inmeterialinspection());
                        details.setInmeterialInspectionMethodName(orderDetail.getPurchaseorderdetails_inmeterialinspectionmethod_name());
                        details.setInmeterialInspectionSamplingRate(orderDetail.getPurchaseorderdetails_inmeterialinspectionsamplingrate());
                        details.setIsclose(orderDetail.getPurchaseorderdetails_isclose());
                        details.setClosedBy(orderDetail.getPurchaseorderdetails_closedby());
                        details.setClosedTime(orderDetail.getPurchaseorderdetails_closedtime());
                        details.setCountQuantity(Objects.isNull(orderDetail.getPurchaseorderdetails_countquantity())? null :new BigDecimal(orderDetail.getPurchaseorderdetails_countquantity()));
                        details.setCuminstockShrinkageQuantity(Objects.isNull(orderDetail.getPurchaseorderdetails_cuminstockshrinkagequantity())? null :new BigDecimal(orderDetail.getPurchaseorderdetails_cuminstockshrinkagequantity()));
                        details.setCountQuantity2(Objects.isNull(orderDetail.getPurchaseorderdetails_countquantity2())? null :new BigDecimal(orderDetail.getPurchaseorderdetails_countquantity2()));
                        details.setCuminstockShrinkageQuantity2(Objects.isNull(orderDetail.getPurchaseorderdetails_cuminstockshrinkagequantity2())? null :new BigDecimal(orderDetail.getPurchaseorderdetails_cuminstockshrinkagequantity2()));
                        details.setOrigPayreqAmount(Objects.isNull(orderDetail.getPurchaseorderdetails_origpayreqamount())? null :new BigDecimal(orderDetail.getPurchaseorderdetails_origpayreqamount()));
                        details.setPayreqAmount(Objects.isNull(orderDetail.getPurchaseorderdetails_payreqamount())? null :new BigDecimal(orderDetail.getPurchaseorderdetails_payreqamount()));
                        details.setExecutableQuantity(Objects.isNull(orderDetail.getPurchaseorderdetails_executablequantity())? null :new BigDecimal(orderDetail.getPurchaseorderdetails_executablequantity()));
                        details.setExecutableQuantity2(Objects.isNull(orderDetail.getPurchaseorderdetails_executablequantity2())? null :new BigDecimal(orderDetail.getPurchaseorderdetails_executablequantity2()));
                        details.setSourceVouchertypeName(orderDetail.getPurchaseorderdetails_sourcevouchertype_name());
                        details.setSourceVouchercode(orderDetail.getPurchaseorderdetails_sourcevouchercode());
                        details.setPriceDimensionName(orderDetail.getPurchaseorderdetails_pricedimension_name());
                        details.setSaleorderCode(orderDetail.getPurchaseorderdetails_saleordercode());
                        details.setCode(orderDetail.getPurchaseorderdetails_code());
                        details.setPriuserdefnvc1(orderDetail.getPurchaseorderdetails_priuserdefnvc1());
                        details.setPriuserdefnvc2(orderDetail.getPurchaseorderdetails_priuserdefnvc2());
                        details.setPriuserdefnvc3(orderDetail.getPurchaseorderdetails_priuserdefnvc3());
                        details.setPriuserdefnvc4(orderDetail.getPurchaseorderdetails_priuserdefnvc4());
                        details.setPriuserdefdecm1(Objects.isNull(orderDetail.getPurchaseorderdetails_priuserdefdecm1())? null :new BigDecimal(orderDetail.getPurchaseorderdetails_priuserdefdecm1()));
                        details.setPriuserdefdecm2(Objects.isNull(orderDetail.getPurchaseorderdetails_priuserdefdecm2())? null :new BigDecimal(orderDetail.getPurchaseorderdetails_priuserdefdecm2()));
                        details.setPriuserdefdecm3(Objects.isNull(orderDetail.getPurchaseorderdetails_priuserdefdecm3())? null :new BigDecimal(orderDetail.getPurchaseorderdetails_priuserdefdecm3()));
                        details.setPriuserdefdecm4(Objects.isNull(orderDetail.getPurchaseorderdetails_priuserdefdecm4())? null :new BigDecimal(orderDetail.getPurchaseorderdetails_priuserdefdecm4()));
                        details.setPubuserdefnvc1(orderDetail.getPurchaseorderdetails_pubuserdefnvc1());
                        details.setPubuserdefnvc2(orderDetail.getPurchaseorderdetails_pubuserdefnvc2());
                        details.setPubuserdefnvc3(orderDetail.getPurchaseorderdetails_pubuserdefnvc3());
                        details.setPubuserdefnvc4(orderDetail.getPurchaseorderdetails_pubuserdefnvc4());
                        details.setPubuserdefdecm1(Objects.isNull(orderDetail.getPurchaseorderdetails_pubuserdefdecm1())? null :new BigDecimal(orderDetail.getPurchaseorderdetails_pubuserdefdecm1()));
                        details.setPubuserdefdecm2(Objects.isNull(orderDetail.getPurchaseorderdetails_pubuserdefdecm2())? null :new BigDecimal(orderDetail.getPurchaseorderdetails_pubuserdefdecm2()));
                        details.setPubuserdefdecm3(Objects.isNull(orderDetail.getPurchaseorderdetails_pubuserdefdecm3())? null :new BigDecimal(orderDetail.getPurchaseorderdetails_pubuserdefdecm3()));
                        details.setPubuserdefdecm4(Objects.isNull(orderDetail.getPurchaseorderdetails_pubuserdefdecm4())? null :new BigDecimal(orderDetail.getPurchaseorderdetails_pubuserdefdecm4()));
                        details.setPurchaseOrderDetailsId(orderDetail.getPurchaseorderdetails_id());
                        details.setDetailMemo(orderDetail.getPurchaseorderdetails_detailmemo());
                        details.setCollaborationtypeName(orderDetail.getPurchaseorderdetails_collaborationtype_name());
                        details.setEntrustedRoleName(orderDetail.getPurchaseorderdetails_entrustedrole_name());
                        details.setOppositeSidemarketingOrganName(orderDetail.getPurchaseorderdetails_oppositesidemarketingorgan_name());
                        details.setCollaborativePriceTrategyName(orderDetail.getPurchaseorderdetails_collaborativepricetrategy_name());
                        details.setEntrustedPurchaserName(orderDetail.getPurchaseorderdetails_entrustedpurchaser_name());
                        details.setCommissionedPurchasePrice(Objects.isNull(orderDetail.getPurchaseorderdetails_commissionedpurchaseprice())? null :new BigDecimal(orderDetail.getPurchaseorderdetails_commissionedpurchaseprice()));
                        details.setProxyCustomerName(orderDetail.getPurchaseorderdetails_proxycustomername());
                        details.setProxyCustomerLinkman(orderDetail.getPurchaseorderdetails_proxycustomerlinkman());
                        details.setProxyCustomerLinkphoneno(orderDetail.getPurchaseorderdetails_proxycustomerlinkphoneno());
                        details.setProxyCustomerPhoneno(orderDetail.getPurchaseorderdetails_proxycustomerphoneno());
                        details.setProxyCustomerAddress(orderDetail.getPurchaseorderdetails_proxycustomeraddress());
                        details.setCollaborateVoucherCode(orderDetail.getPurchaseorderdetails_collaboratevouchercode());
                        details.setOppositeSidevoucherId(orderDetail.getPurchaseorderdetails_oppositesidevoucherid());
                        purchaseOrderDetails.add(details);

                    }
                }

                saveOrUpdateBatch(purchaseOrder);
                purchaseOrderDetailsService.saveOrUpdateBatch(purchaseOrderDetails);

            }
        } catch (ChanjetApiException e) {
            throw new RuntimeException(e);
        }

    }
}
